home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 October: Mac OS SDK / Dev.CD Oct 00 SDK1.toast / Development Kits / Mac OS / Translation Manager / Sample Code / Translation Extension Example / TranslationComponent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-05  |  5.0 KB  |  145 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        TranslationComponent.c 
  3.  
  4.     Contains:    Source code of a component shell for a translation extension
  5.     
  6.     Copyright:    © 1994,1998 by Apple Computer, Inc., all rights reserved.
  7. */
  8.  
  9. // ------------------------------------------------------------------------------------
  10. //        
  11. //        This file only implements the skeleton of a component, which can be used as the
  12. //    shell of a translation extension.
  13. //
  14. //        Since we will implement a FAT component, This code will be used to generate 
  15. //    two code resources, one for 68K, one for PowerPC.
  16. //
  17. //        One noticeable difference between the 68K code and PowerPC code is that any- 
  18. //    time your code uses a callback function(ProcPtr), you must use a UniversalProcPtr 
  19. //    in the PowerPC code instead. So when your native component dispatches to internal 
  20. //    functions using CallComponentFunction or CallComponentFunctionWithStorage you must  
  21. //    use a UniversalProcPtr.
  22. //
  23. //        There are two methods to build a UniversalProcPtr, one is using NewRoutine-
  24. //    Descriptor, another is declaring a global RoutineDescriptor for this code fragment. 
  25. //    The disadvantage for the first method is that you must call DisposeRoutineDescriptor 
  26. //    to dispose of routine descriptors you have created.
  27.  
  28. #include <Types.h>
  29. #include <Files.h>
  30. #include <MixedMode.h>
  31. #include <Traps.h>
  32. #include <Errors.h>
  33. #include <Memory.h>
  34. #include <Resources.h>
  35. #include <Components.h>
  36. #include <TranslationExtensions.h>
  37.  
  38. #include "TranslationComponent.h"
  39.  
  40. // ------------------------------------------------------------------------------------------
  41. //    Global routine descriptor for call back function.
  42.  
  43. #ifdef powerc
  44.     INSTANTIATE_ROUTINE_DESCRIPTOR(DoGetFileTranslationList);
  45.     INSTANTIATE_ROUTINE_DESCRIPTOR(DoIdentifyFile);
  46.     INSTANTIATE_ROUTINE_DESCRIPTOR(DoTranslateFile);
  47.     INSTANTIATE_ROUTINE_DESCRIPTOR(DoGetScrapTranslationList);
  48.     INSTANTIATE_ROUTINE_DESCRIPTOR(DoIdentifyScrap);
  49.     INSTANTIATE_ROUTINE_DESCRIPTOR(DoTranslateScrap);
  50.     INSTANTIATE_ROUTINE_DESCRIPTOR(DoGetTranslatedFilename);
  51. #endif
  52.  
  53.  
  54. // ------------------------------------------------------------------------------------------
  55. //
  56. //        This is the actual entry point for the translation extension.  It handles some Component     
  57. //    Manager setup, but its main task is to dispatch to the extension subroutines when known             
  58. //    selectors are encountered.    
  59. //
  60. //
  61. pascal ComponentResult main( ComponentParameters *params, Handle storage )
  62. {
  63.  
  64.     ComponentResult            result;
  65.  
  66.     switch (params->what)
  67.     {
  68.         
  69.         case kComponentOpenSelect:                //    Allocate memory for storage when component is opened    
  70.                 {    ComponentInstance    self = (ComponentInstance)params->params[0];
  71.                     Handle                h      = NewHandle(sizeof(ComponentInstance));    // allocate storage
  72.                     
  73.                     if ( h != NULL )
  74.                     {
  75.                         (**(ComponentInstance**)h) = self;              // put instance in storage
  76.                         SetComponentInstanceStorage(self, h);
  77.                         result = noErr;
  78.                     }
  79.                     else 
  80.                         result = MemError();
  81.                 }                
  82.                 break;
  83.                 
  84.         case kComponentCloseSelect:                //    Clean up memory when component is closed
  85.                 if( storage != nil )
  86.                     DisposeHandle(storage);
  87.                 result = noErr;
  88.                 break;
  89.                 
  90.         case kComponentCanDoSelect:                //    Component Manager needs to know what selectors we can handle
  91.                 {
  92.                     short    selector = *(short*)params->params;
  93.  
  94.                     if ( (( kComponentVersionSelect <= selector) && (selector <= kComponentOpenSelect))
  95.                           || ((kTranslateGetFileTranslationList  <= selector) && (selector <= kTranslateTranslateFile))
  96.                           || ((kTranslateGetScrapTranslationList <= selector) && (selector <= kTranslateTranslateScrap)) )
  97.                         result = 1;
  98.                     else
  99.                         result = 0;          
  100.                 };
  101.                 break;
  102.         case kComponentVersionSelect:            //    Which version of this component 
  103.                 result = 0;        
  104.                 break;
  105.                 
  106.         case kTranslateGetFileTranslationList:    //    Want file translation list 
  107.                 result = CallComponentFunctionWithStorageUniv((Handle)**(ComponentInstance**)storage, params
  108.                                                                             , DoGetFileTranslationList);
  109.                 break;    
  110.  
  111.         case kTranslateIdentifyFile:            //    Want to identify file?
  112.                 result = CallComponentFunctionWithStorageUniv((Handle)**(ComponentInstance**)storage, params
  113.                                                                             , DoIdentifyFile);
  114.                 break;    
  115.  
  116.         case kTranslateTranslateFile:            //    Want to translate file?
  117.                 result = CallComponentFunctionWithStorageUniv((Handle)**(ComponentInstance**)storage, params
  118.                                                                             , DoTranslateFile);
  119.                 break;    
  120.         
  121.         case kTranslateGetScrapTranslationList:    //    Want scrap translation list?
  122.                 result = CallComponentFunctionWithStorageUniv((Handle)**(ComponentInstance**)storage, params
  123.                                                                             , DoGetScrapTranslationList);
  124.                 break;    
  125.  
  126.          case kTranslateIdentifyScrap:            //    Want to identify scrap?    
  127.                 result = CallComponentFunctionWithStorageUniv((Handle)**(ComponentInstance**)storage, params
  128.                                                                             , DoIdentifyScrap);
  129.                 break;    
  130.  
  131.         case kTranslateTranslateScrap:            //    Want to translate scrap?
  132.                 result = CallComponentFunctionWithStorageUniv((Handle)**(ComponentInstance**)storage, params
  133.                                                                             , DoTranslateScrap);
  134.                 break;    
  135.  
  136.         default:
  137.                 result = badComponentSelector;
  138.                 
  139.     };
  140.     
  141.     return result;
  142. }
  143.  
  144.  
  145.